AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua')

// follows a target, aims ahead, + blows it up
// has "dumb" follow, so when the conditions are right, they can get the missiles to circle around the object, instead of hitting it
// the same way you can get the RPG to circle around an object 

function ENT:Initialize()
	util.PrecacheModel("models/items/AR2_Grenade.mdl")
	self:SetModel( "models/items/AR2_Grenade.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	self:StartMotionController()

	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:EnableGravity(true)
	end

	self.Target = self:LockTarget(self:GetPos(), self:GetForward() * 9999)
	if not self.Target:IsValid() then self.Target = nil end
	self.Terminate = false
	self.Ahead = false
	self.Dead = false
	self.Deploy = CurTime() + 2
	self.Speed = 2000
	self.Color = team.GetColor(self:GetOwner():Team())

	self.Trail = util.SpriteTrail(self, 0, self.Color, false, 5, 2, 1, 1/(16+16)*0.5, "trails/plasma.vmt")
end

function ENT:PhysicsSimulate(phys, frametime)
	if self.Dead then return end
	phys:Wake()
	if self:WaterLevel() > 0 then self.Terminate = true end
	if self.Terminate then self:DoExplode() return end
	if CurTime() < self.Deploy then phys:ApplyForceCenter(self:GetAngles():Forward():Normalize() * 3000 ) return end
	if self.Target then
		// this missile tries to fly ahead of the aircraft, + explode
		local class = self.Target:GetClass()
		local spos = self:GetPos()
		local tpos = self.Target:GetPos()
		local vangs = self.Target:GetAngles()
		local vforward = vangs:Forward()
		if class == "vehicle_*" then
			vangs = self.Target:GetTrueAngles()
			vforward = vangs:GetTrueForward()
		end
		local diff = spos - tpos

		if not self.Ahead then
			if vforward:Normalize():Dot(diff:Normalize()) > 0.7 then
				self.Ahead = true
				self.Speed = self.Speed * 1.5
			else
				tpos = tpos + (vforward * 250)
			end
		end

		dpos = (tpos - spos):Normalize() * self.Speed
		phys:ApplyForceCenter(dpos)
	else
		phys:ApplyForceCenter(self:GetAngles():Forward():Normalize() * self.Speed)
	end
end

function ENT:DoExplode()
	local expl = {"ambient/levels/prison/inside_battle8.wav",
	"weapons/explode3.wav",
	"weapons/explode4.wav",
	"weapons/explode5.wav"}
	
	local rexpl = expl[math.random(1,4)]
	util.PrecacheSound(rexpl)
	
	self:ExplosionDamage(self:GetOwner(),self:GetPos(),math.random(10,15),120,2000,rexpl,math.random(125,175))

	local edata = EffectData()
	edata:SetOrigin(self:GetPos())
	util.Effect("dhs_explosion", edata)

	//self:Remove()  This causes spam
	self.Trail:Fire("kill",1,0.001)
	self:Fire("kill",1,0.0001)
	self.Dead = true
end

function ENT:PhysicsCollide( data, physobj )
	// hit the ground, hit the target, who knows
	self.Terminate = true
end

function ENT:SetPlayer(ply)
	if not ply then self:Remove() end
	self:SetPhysicsAttacker(ply)
	self:SetOwner(ply)	
end